package sunyu.demo.mina;

import sunyu.toolkit.core.*;
import sunyu.toolkit.mina.SocketClientTemplate;

import java.io.File;
import java.io.IOException;
import java.util.Date;
import java.util.concurrent.TimeUnit;

/**
 * @author 孙宇
 */
public class MainExecute {

    static int loginNum = 1;//登入登出流水号

    static String loginName = "RC_EKEC00000";
    static String loginPassword = "RC_12345600000000000";
    static String vin = "LR83STG64GBS16116";
    static String did = "homer123456789032";
    static String ICCID = "898602B4151651692487";

    static String 国标网关IP = "218.205.176.44";
    static int 国标网关端口 = 19007;
    static String 公司转发网关IP = "192.168.11.109";
    static int 公司转发网关端口 = 19006;

    public static void main(String[] args) throws IOException, InterruptedException {
        System.out.println(args.length);
        System.out.println(JsonKit.toJson(args));
        if (args.length < 2) {
            System.out.println("参数格式：命令编号  参数1  参数2  参数N");
            System.out.println();

            System.out.println("平台登入登出  参数格式：命令编号  DID  VIN  ICCID  导出日志位置  [起始流水号]  [网关登录名]  [网关登录密码]");
            System.out.println();

            System.out.println("平台登入登出举例：");
            System.out.println("1 homer123456789 vin123456677 iccid12345677 d:/1.log 1 RC_EKEC00000 RC_12345600000000000");
            System.out.println();

            System.out.println("232302改232303  参数格式：命令编号  原日志  新日志");
            System.out.println();

            System.out.println("232302改232303举例：");
            System.out.println("2 d:/old.log d:/new.log");
            System.out.println();

            System.exit(0);
        }
        if (args[0].equals("1") && (args.length == 8 || args.length == 5)) {//平台登入登出
            did = args[1];
            vin = args[2];
            ICCID = args[3];
            File f = new File(args[4]);
            FileKit.deleteFile(f);
            FileKit.createNewFile(f);
            if (args.length == 8) {
                loginNum = Integer.parseInt(args[5]);
                loginName = args[6];
                loginPassword = args[7];
            }

            SocketClientTemplate client = new ProtocolSocketClient(国标网关IP, 国标网关端口, f);//直接往国标网关发送

            for (int i = 1; i <= 5; i++) {
                loginNum = 平台登入(f, client, loginNum);
                TimeUnit.SECONDS.sleep(5);

                loginNum = 平台登出(f, client, loginNum);
                TimeUnit.SECONDS.sleep(5);

                loginNum++;
            }
            TimeUnit.SECONDS.sleep(1);

            client.getConnector().dispose(true);//释放资源

            System.exit(0);
        }

        if (args[0].equals("2") && args.length == 3) {
            File log = new File(args[2]);
            FileKit.deleteFile(log);
            FileKit.createNewFile(log);

            FileKit.readLine(new File(args[1]), obj -> {
                byte[] bytes = new byte[0];
                报文结构 bw = ProtocolKit.获得报文结构(obj.toUpperCase());

                bytes = ByteKit.byteMerger(bytes, bw.起始符);
                bytes = ByteKit.byteMerger(bytes, 命令标识.补发信息上报);
                bytes = ByteKit.byteMerger(bytes, 应答标识.命令);
                bytes = ByteKit.byteMerger(bytes, bw.唯一识别码);
                bytes = ByteKit.byteMerger(bytes, bw.数据单元加密方式);
                bytes = ByteKit.byteMerger(bytes, bw.数据单元长度);
                bytes = ByteKit.byteMerger(bytes, bw.数据单元.实时信息上报.数据采集时间);
                bytes = ByteKit.byteMerger(bytes, bw.数据单元.实时信息上报.cans);
                bytes = ProtocolKit.BBC(bytes);

                String hex = StringKit.bytes2hexString(bytes);
                FileKit.appendContentAndRN(log, hex);
            });

            System.exit(0);
        }
    }

    private static int 平台登入(File f, SocketClientTemplate client, int loginNum) {
        byte[] bytes;
        bytes = ProtocolKit.平台登入(TimeKit.format("yyyyMMddHHmmss", new Date()), loginNum, loginName, loginPassword);
        FileKit.appendContentAndRN(f, "[" + TimeKit.format("yyyy-MM-dd HH:mm:ss", new Date()) + "] [平台登入] " + StringKit.bytes2hexString(bytes));
        client.sendBytes(bytes);
        return loginNum;
    }

    private static int 平台登出(File f, SocketClientTemplate client, int loginNum) {
        byte[] bytes;
        bytes = ProtocolKit.平台登出(TimeKit.format("yyyyMMddHHmmss", new Date()), loginNum);
        FileKit.appendContentAndRN(f, "[" + TimeKit.format("yyyy-MM-dd HH:mm:ss", new Date()) + "] [平台登出] " + StringKit.bytes2hexString(bytes));
        client.sendBytes(bytes);
        return loginNum;
    }

}
